package com.taomu.app.zongchouhuli.moudle.mine.contract.impl;

import android.util.Log;

import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.entity.BaseResponse;
import com.taomu.app.zongchouhuli.http.api.HttpObserver;
import com.taomu.app.zongchouhuli.http.api.Request;
import com.taomu.app.zongchouhuli.http.contract.RxJavaBaseCallback;
import com.taomu.app.zongchouhuli.http.res.ResMonthDistributeFeeEntity;
import com.taomu.app.zongchouhuli.http.res.ResProductEntity;
import com.taomu.app.zongchouhuli.http.res.ResSpreadEntity;
import com.taomu.app.zongchouhuli.http.res.ResUserInfoEntity;
import com.taomu.app.zongchouhuli.http.utls.RxSchedulerHelper;
import com.taomu.app.zongchouhuli.moudle.mine.contract.UserSpreadContract;
import com.taomu.app.zongchouhuli.moudle.mine.entity.SpreadEntity;
import com.taomu.app.zongchouhuli.utils.ParseUtil;
import com.taomu.app.zongchouhuli.utils.StorageHelper;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class UserSpreadPresenter implements UserSpreadContract.Presenter {

    private UserSpreadContract.View mContractView;

    private CompositeDisposable disposable;

    public UserSpreadPresenter(UserSpreadContract.View mContractView){
        this.mContractView = mContractView;
        this.disposable = new CompositeDisposable();
    }

    @Override
    public void getMonthSpreadMoney() {
        Request.getIntence().getThisMonthDistributeFee(StorageHelper.getToken())
                .flatMap(new Function<String, Observable<ResMonthDistributeFeeEntity>>() {
                    @Override
                    public Observable<ResMonthDistributeFeeEntity> apply(String s) throws Exception {
                        ResMonthDistributeFeeEntity res = ParseUtil.parseObject(s, ResMonthDistributeFeeEntity.class);
                        return Observable.just(res);
                    }
                })
                .compose(RxSchedulerHelper.<ResMonthDistributeFeeEntity>transMain())
                .subscribe(new HttpObserver<ResMonthDistributeFeeEntity>(mContractView,disposable,true) {
                    @Override
                    public void onNext(ResMonthDistributeFeeEntity msg) {
                        if(mContractView != null){
                            mContractView.setMonthTotal(msg);
                        }
                    }
                });
    }

    @Override
    public void getSpreadListData(int ship,int take) {
        Request.getIntence().getMyDistribute(StorageHelper.getToken(), String.valueOf(ship), String.valueOf(take))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<ResSpreadEntity>(mContractView,disposable,true) {
                    @Override
                    public void onNext(ResSpreadEntity resProductEntities) {
                        Log.d("wqdqwdwqdwqdwq","onNext:"+resProductEntities.getData().size());
                        if(mContractView != null){
                            mContractView.setSpreadListData(resProductEntities);
                        }
                    }
                });
        //setSpreadListData();
    }

    @Override
    public void detch() {

    }

    @Override
    public void tixian(String moneystr) {
        Request.getIntence().getTranferProductAccount(moneystr, "2")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<String>(mContractView,disposable,true) {
                    @Override
                    public void onNext(String baseResponse) {
                        if(mContractView != null){
                            mContractView.setMsg(baseResponse);
                        }
                    }
                });
    }

    @Override
    public void getUserInfo() {
        Request.getIntence().getUserInfo(StorageHelper.getToken())
                .flatMap(new Function<String, ObservableSource<BaseChangeResponse<ResUserInfoEntity>>>() {
                    @Override
                    public ObservableSource<BaseChangeResponse<ResUserInfoEntity>> apply(String json) throws Exception {
                        BaseResponse<com.alibaba.fastjson.JSONObject> baseResponse = ParseUtil.parseObject(json,BaseResponse.class);
                        BaseChangeResponse<ResUserInfoEntity> data = new BaseChangeResponse<>();
                        data.setData(ParseUtil.parseObject(baseResponse.getData().toString(),ResUserInfoEntity.class));
                        data.setCount(baseResponse.getCount());
                        return Observable.just(data);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseChangeResponse<ResUserInfoEntity>>(mContractView,disposable,true) {
                    @Override
                    public void onNext(BaseChangeResponse<ResUserInfoEntity> baseResponse) {
                        if(mContractView != null){
                            StorageHelper.saveToken(baseResponse.getData().getToken());
                            StorageHelper.saveAccountInfo(ParseUtil.toJson(baseResponse.getData()));
                            mContractView.setTotalMoney(baseResponse.getData().getDistribute_account());
                        }
                    }
                });
    }

    private void setSpreadListData(){
        List<SpreadEntity> mData = new ArrayList<>();
        for(int i=0;i<8;i++){
            SpreadEntity entity = new SpreadEntity();
            entity.setProductName("产品名称");
            entity.setIncome("100");
            entity.setBuyer("大*");
            entity.setTime("2018/08/31");
            mData.add(entity);
        }
        if(mContractView != null){
            //mContractView.setSpreadListData(mData);
        }
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDetch() {
        mContractView = null;
        if(disposable != null){
            disposable.dispose();
            disposable = null;
        }
    }
}
